package com.lzy.wzy.service.imp;

import com.lzy.wzy.config.MyPasswordEncoder;
import com.lzy.wzy.constant.MyConstant;
import com.lzy.wzy.mapper.TemplateMapper;
import com.lzy.wzy.mapper.UserMapper;
import com.lzy.wzy.mapper.WorkMapper;
import com.lzy.wzy.model.LevelOneTemplateBean;
import com.lzy.wzy.result.ResponseCode;
import com.lzy.wzy.result.Result;
import com.lzy.wzy.service.IndexService;
import com.lzy.wzy.token.MyUsernamePasswordToken;
import com.lzy.wzy.utils.PatternUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class IndexServiceImp implements IndexService {
    @Autowired
    private TemplateMapper templateMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private WorkMapper workMapper;

    @Autowired
    private MyPasswordEncoder myPasswordEncoder;


    /*
     * @Author wzy
     * @Description //TODO 获取此工单下编码绑定的模板，返修工单排外
     * @Date 2021/8/20 17:28
     * @Param * @param: workOrderNumber 工单号
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    @Override
    public List<LevelOneTemplateBean> issueTemplate(String workOrderNumber) {
        List<LevelOneTemplateBean> list = new ArrayList<>();
        List<LevelOneTemplateBean> order = new ArrayList<>();
        if (!PatternUtils.checkWorkOrderNumber(workOrderNumber)) {
            return list;
        }
        String status = workMapper.getWorkOrderStatus(workOrderNumber);
        if (workOrderNumber.startsWith("F")) {
            LevelOneTemplateBean reworkOrderTemplate;
            if (MyConstant.WorkOrderStatus.FINISHED.equals(status) || MyConstant.WorkOrderStatus.CASECLOSE.equals(status)){
                reworkOrderTemplate = templateMapper.getAReworkOrderTemplate(workOrderNumber);
            }else {
                reworkOrderTemplate = templateMapper.getAReworkOrderEnabledTemplate(workOrderNumber);
            }
            if (null != reworkOrderTemplate) {
                list.add(reworkOrderTemplate);
            }
        } else {
            if (MyConstant.WorkOrderStatus.FINISHED.equals(status) || MyConstant.WorkOrderStatus.CASECLOSE.equals(status)){
                list = templateMapper.getWorkOrderTemplate(workOrderNumber);
            }else {
                list = templateMapper.getWorkOrderEnabledTemplate(workOrderNumber);
            }
        }
        if (!list.isEmpty()) {
            String tableName;
            LevelOneTemplateBean templateBean;
            Iterator<LevelOneTemplateBean> iterator = list.iterator();
            while (iterator.hasNext()) {
                templateBean=iterator.next();
                tableName=templateMapper.queryTableNameByTemplateId(templateBean.getTemplateId());
                if (templateMapper.getMachinesCountByWorkOrderNumber(tableName, workOrderNumber)>0) {
                    order.add(templateBean);
                    iterator.remove();
                }
            }
        }
        order.addAll(list);
        return order;
    }

    /*
     * @Author wzy
     * @Description //TODO 修改密码
     * @Date 2021/8/20 17:29
     * @Param * @param: password
     * @return com.lzy.wzy.result.Response<org.apache.poi.ss.formula.functions.T>
     **/
    @Override
    public Result<T> doUpdatePassword(String password) {
        MyUsernamePasswordToken token = (MyUsernamePasswordToken) SecurityContextHolder.getContext().getAuthentication();
        String userId = (String) token.getPrincipal();
        String oldPassword = userMapper.getPasswordByUserId(userId);
        if (null != oldPassword) {
            if (!myPasswordEncoder.matches(oldPassword, password) && userMapper.updatePassword(myPasswordEncoder.encode(password), userId) > 0) {
                return Result.success( "修改成功");
            } else {
                return Result.failure("密码不能与原密码一致");
            }
        }
        return Result.failure("修改失败");
    }

    @Override
    public Result<String> doGetName() {
        MyUsernamePasswordToken token = (MyUsernamePasswordToken) SecurityContextHolder.getContext().getAuthentication();
        String userId = (String) token.getPrincipal();
        String name = userMapper.queryNameByUserId(userId);
        return Result.to(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getMsg(),name);
    }


}
